// pages/AnswerDtails/index.js
import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    return_index:false,
    datas: [],
    ModalDatas: [],
    is_return:0,
    return_desc:'',
    questionnaire:[],
    qid:'',
    price:0,
    AnswerShow:false,
    modledata: {
      return_name:'',
      return_phone:'',
      return_wecaht:''
    },
    modledatashow:false
    // options:{
    //   pid: [],
    //   type: {},
    //   aid: {},
    //   return_name: '',
    //   return_phone: '',
    //   return_wecaht: '',
    //   qid: ''
    // },
    //ids:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function(res) {
    //console.log(res)
    this.setData({
      qid: res.datas
    })
    Myhttp.request({
      url: 'questionnaire/index/questionnaireInfo',
      method: 'POST',
      data: {
        qid: this.data.qid
      },
      success: (res) => {

          console.log(res)
         if(res.data.code == 200){
           let answer = []
           answer.push(res.data.data.info)
          //  answer.forEach(val => {
          //    val.src = val.url_domin + val.cover
          //  })
           this.setData({
             questionnaire: answer,
             datas: res.data.data.problem_list,
             is_return: res.data.data.info.is_return,
             return_desc: res.data.data.info.return_desc,
             price: res.data.data.info.price
           })
         }else{
           wx.showToast({
             title:res.data.msg
           })
         }
        
        //console.log(res.data.data)
      }
    })
  },
  inputNum:function(e){
    console.log(e)
    this.setData({
      AnswerShow: false,
      modledatashow:true,
      modledata:e.detail.data,
    })
  },
  final: Myhttp.throttle(function (e) {
    //console.log(this.data.datas)
    let option = {
      pid: [],
      type: {},
      aid: {},
      price:0,
      return_name: '',
      return_phone: '',
      return_wecaht: '',
      qid: ''
    }

    let modledata = this.data.modledata
    
    option.return_name = modledata.return_name
    option.return_phone = modledata.return_phone
    option.return_wecaht = modledata.return_wecaht
    
    let datas = this.data.datas
    let newdata = []
    for (let i = 0; i < datas.length;i++){
      let IA = i
      let aa = false
      for (let i = 0; i < datas[IA].answer.length; i++) {
        if (datas[IA].answer[i].checked) {
          aa = true
        }
      }
      aa ? newdata.push(datas[IA]) : '' 
    }

    // console.log(newdata)

    newdata.forEach((val,index)=>{
      let indexa = index
      let vala = val

      option.pid.push(val.id)
      option.type[val.id] = val.type
      option.aid[val.id] = []
      
      val.answer.forEach((val, index) => {
       if(val.checked){
          option.aid[vala.id].push(val.id)
       }
      })
     // option.type[val.pid](val.type)
    })
    option.qid = this.data.qid
    option.price = this.data.price
    // console.log(option)

    Myhttp.request({
      url: 'questionnaire/index/submitAnswer',
      method: 'POST',
      data: option,
      success: (res) => {
        // console.log(res)
        if(res.data.code == 200){
          Myhttp.jump({
            method: 'reLaunch',
            url: `/pages/submitOk/index?datas=${res.data.data.pricce}`
          })
        }else{
          wx.showToast({
            title:res.data.msg
          })
        }
      }
    })
  },1000),
  radioChange: function (e) { 
    let pid = e.currentTarget.dataset.pid.toString()
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let datas = this.data.datas
    // console.log(index)
    for (let i = 0; i < datas[index].answer.length; i++) {
      if (datas[index].answer[i].id == id) {
        datas[index].answer[i].checked = true
      } else {
        datas[index].answer[i].checked = false
      }
    }

    this.setData({
      datas:datas
    })
    //console.log(datas)
  },
  checkboxChange: function (e) {
    //console.log(e)
    let pid = e.currentTarget.dataset.pid.toString()
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let datas = this.data.datas
    
    for (let i = 0; i < datas[index].answer.length; i++) {
      if (datas[index].answer[i].id == id) {
        datas[index].answer[i].checked = !datas[index].answer[i].checked
      }
    }
    this.setData({
      datas: datas
    })
    //console.log(datas)
  },
  AllowModal: function () {
    this.setData({
      AnswerShow: true
    })
    // console.log(this.data.modledata)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: `/pages/StartAnswer/AnswerDtails/index?datas=${this.data.qid}`
    }
  }
})